/*****************************************************************************
 * lwgy.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
#include "thirdparty/modbus/modbus.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_INFO
#define LOG_MODULE    "lwgy"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/
typedef enum {
    DEV_LWGY_REG_ANAPPY_H_IDX, /** 瞬时流量高位 */
    DEV_LWGY_REG_ANAPPY_L_IDX, /** 瞬时流量低位 */
    DEV_LWGY_REG_TOTAL_H_IDX,  /** 累计流量高位 */
    DEV_LWGY_REG_TOTAL_L_IDX,  /** 累计流量低位 */
} dev_lwgy_reg_idx_t;

typedef struct {
    plum_u16 addr;
    plum_u16 value;
} dev_lwgy_reg_t;

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/
PLUM_PRIVATE
dev_lwgy_reg_t reg_tb[] = {
    [DEV_LWGY_REG_ANAPPY_H_IDX] = {0x0001, 0},
    [DEV_LWGY_REG_ANAPPY_L_IDX] = {0x0002, 0},
    [DEV_LWGY_REG_TOTAL_H_IDX]  = {0x0003, 0},
    [DEV_LWGY_REG_TOTAL_L_IDX]  = {0x0004, 0},
};

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
plum_void dev_flow_meter_lwgy_notice(modbus_protocol_evt evt, plum_void *arg)
{
    switch (evt) {
        case MODBUS_EVT_MSG:
        {
            modbus_msg_t *msg = (modbus_msg_t *)arg;
            plum_u8       loop;
            for (loop = 0; loop < PLUM_ARRAY_CNT(reg_tb); loop++) {
                if (reg_tb[loop].addr == msg->addr) {
                    reg_tb[loop].value = *(plum_u16 *)msg->value;
                    LOG_D("addr %04X , value : %04X", reg_tb[loop].addr,
                          reg_tb[loop].value);
                    break;
                }
            }
        } break;

        case MODBUS_EVT_ERR:
        {
            modbus_err_t *msg = (modbus_err_t *)arg;
            LOG_E("modbus err:%s", msg->discr);
        } break;

        default:
            LOG_E("dev_flow_meter_lwgy_notice err evt");
            break;
    }
}
/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
int dev_flow_meter_init(int (*io_read)(unsigned char *data, unsigned int len,
                                       unsigned int timeout),
                        int (*io_write)(unsigned char *data, unsigned int len))
{
    plum_s32 rc = PLUM_ECODE_OK;

    PLUM_UNUSED(io_read);
    PLUM_UNUSED(io_write);

    return (rc);
}

PLUM_PUBLIC
float dev_flow_meter_anappy_read(unsigned char id)
{
    do {
        plum_s32 rc = PLUM_ECODE_OK;

        rc = modbus_protocol_register_notice(dev_flow_meter_lwgy_notice);
        if (rc) {
            LOG_E("modbus_protocol_register_notice err,rc: %d", rc);
            break;
        }

        rc = modbus_protocol_read(MODBUS_PROTOCOL_RTU, id,
                                  MODBUS_PROTOCOL_READ_SHOLD,
                                  reg_tb[DEV_LWGY_REG_ANAPPY_H_IDX].addr, 2);
        if (rc) {
            LOG_E("modbus_protocol_read err,rc: %d", rc);
            break;
        }
    } while (0);

    unsigned int value = ((reg_tb[DEV_LWGY_REG_ANAPPY_H_IDX].value << 16) |
                          reg_tb[DEV_LWGY_REG_ANAPPY_H_IDX].value);

    return (*(float *)&value);
}

float dev_flow_meter_total_read(unsigned char id)
{
    do {
        plum_s32 rc = PLUM_ECODE_OK;

        rc = modbus_protocol_register_notice(dev_flow_meter_lwgy_notice);
        if (rc) {
            LOG_E("modbus_protocol_register_notice err,rc: %d", rc);
            break;
        }

        rc = modbus_protocol_read(MODBUS_PROTOCOL_RTU, id,
                                  MODBUS_PROTOCOL_READ_SHOLD,
                                  reg_tb[DEV_LWGY_REG_TOTAL_H_IDX].addr, 2);
        if (rc) {
            LOG_E("modbus_protocol_read err,rc: %d", rc);
            break;
        }
    } while (0);

    unsigned int value =
        (float)((reg_tb[DEV_LWGY_REG_TOTAL_H_IDX].value << 16) |
                reg_tb[DEV_LWGY_REG_TOTAL_L_IDX].value);

    return (*(float *)&value);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
